<?php
class Configm extends CI_Model {

    //构造函数
    public function __construct()
    {
		parent::__construct();
	}

    //系统设置列表显示
	public function index()
	{
        $sql = "select * from mifi_config";	
        $config = $this->db->query($sql)->result_array();
        $info = array();
        if(!empty($config)){
	        foreach ($config as $key => $val) {
                $info[$val['name']] = $val['value'];
	       }
            echo json_encode(['code'=>1,'data'=>$info]);
        }else{
      	    echo json_encode(['code'=>0]);
        }
	}

	//系统系统设置信息修改
	public function update()
	{
		$info =  $this->input->post();
		$open = $this->input->post('open', true);
		if(empty($open)){
			$info['open'] = 0;
		}else{
			$info['open'] = 1;
		}
		$open = $this->input->post('open', true);
		foreach ($info as $key => $value){
			$config = $this->db->update('config', array('value'=> $value), array('name'=> $key));
		}
		echo json_encode(['code'=>1,'infomsg'=>'系统设置修改成功']);
	}

	//上传图片
	public function upload()
	{
		//上传图片失败
	    if($_FILES['file']['error']){
		    echo json_encode(['code'=>0,'infomsg'=>'图片上传失败']);
	    }else{
	    	//上传的图片类型
	    	$type = $_FILES['file']['type'];
	    	if($type == "image/jpeg" || $type == "image/png" || $type == "image/gif" || $type == "image/pjpeg" || $type == "image/jpg"){
	    		//头像储存的路径
	    		$urls = "upload/config";
	    		if(!file_exists($urls)){
                    mkdir($urls,0777,true);
                 }
                  //图片后缀名
                  $img = explode('/', $type);
                  $imgs = $img[1];
                  $date = date('YmdHis').'.';
                  //得到上传文件的名字
                  $fileName = $_FILES['file']['name'];
                  //将文件名以'.'分割得到后缀名,得到一个数组
                  //得到一个新的文件为'20070705163148.jpg',即新的路径
                  $newPath = $date;
                  $upimg = $urls.'/'.$newPath.$imgs;
	    		  $info = move_uploaded_file($_FILES["file"]["tmp_name"],
                   $upimg);
	    		  if($info){
	    		  	  $this->db->update('config', array('value'=> $upimg), array('name'=> 'logo'));
		              echo json_encode(['code'=>1,'infomsg'=>'图片上传成功']);
	    		  }else{
	    		  	  echo json_encode(['code'=>0,'infomsg'=>'图片上传失败']);
	    		  }
	    	}else{
	    		 echo json_encode(['code'=>0,'infomsg'=>'图片文件类型不支持']);
	    	}
	    }
	}

	//添加系统设置
	public function add()
	{
		$config['name'] = trim($this->input->post('name',true));
		$config['value'] = trim($this->input->post('value',true));
		$config['inc_type'] = trim($this->input->post('type',true));
		$config['desc'] = trim($this->input->post('desc',true));
		//判断数据库是否存在name
		$sql = "select * from mifi_config where name = '{$config['name']}'";	
        $row = $this->db->query($sql)->result_array();
        if($row){
        	echo json_encode(['code'=>0,'infomsg'=>'该名称已经存在，请重新输入']);
        	exit;
        }
		$res = $this->db->insert('config', $config); 
		if($res){	
		   echo json_encode(['code'=>1,'infomsg'=>'添加成功']);
		}else{
		   echo json_encode(['code'=>0,'infomsg'=>'添加失败']);
		}
	}

	//短信设置列表显示
    public function smsinfo()
    {
    	$sql = "select * from mifi_smsinfo where id = 1";
    	$smsinfo = $this->db->query($sql)->row_array();
    	if($smsinfo){
    		echo json_encode(['code'=>1,'data'=>$smsinfo]);
    	}else{
    		echo json_encode(['code'=>0,'infomsg'=>'你还未设置短信设置']);
    	}
    }

	//短信名称显示
	public function smsname()
	{
		$sql = "select * from mifi_sms where status = 1 order by id desc";
		$sms = $this->db->query($sql)->result_array();
		if($sms){
            echo json_encode(['code'=>1,'data'=>$sms]);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'还未添加短信平台名称']);
		}
	}

	//添加短信设置
	public function addsms()
	{
		$sms['smsform'] = trim($this->input->post('smsform', true));
		$status = $this->input->post('status', true);
		if(empty($status)){
			$sms['status'] = 0;
		}else{
			$sms['status'] = 1;
		}
		$sms['addtime'] = date('Y-m-d H:i:s');
	/*	$sms['smsform'] = str_replace('{', "'.", $sms['smsform']);
		$sms['smsform'] = str_replace('}', ".'", $sms['smsform']);*/
		//判断数据库是否存在smsform
		$sql = "select * from mifi_sms where smsform = '{$sms['smsform']}'";
		$row = $this->db->query($sql)->result_array();
        if($row){
        	echo json_encode(['code'=>0,'infomsg'=>'该模板已经存在，请重新输入']);
        	exit;
        }
		$res = $this->db->insert('sms', $sms); 
		if($res){
			echo json_encode(['code'=>1,'infomsg'=>'添加成功']);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'添加成功']);
		}
	}

	//修改短信设置
	public function updatesms()
	{
		$upsmsinfo = $this->input->post();
		$usertrue = $this->input->post('usertrue', true);
		if(empty($usertrue)){
			$upsmsinfo['usertrue'] = 0;
		}else{
			$upsmsinfo['usertrue'] = 1;
		}
		$upsmsinfo = $this->db->update('smsinfo',  $upsmsinfo, array('id'=> 1));
		if($upsmsinfo){	
		    echo json_encode(['code'=>1,'infomsg'=>'短信设置修改成功']);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'短信设置修改失败']);
		}
	}

	//模板设置列表
	public function smslist()
	{
		$total = $this->db->count_all_results('sms');
		$page = $_POST['page'];
        $limit = $_POST['limit'];
        $offset = ($page - 1) * $limit;
		$sql = "select * from  mifi_sms order by id desc limit ".$offset.",".$limit."";
		$sms = $this->db->query($sql)->result_array();
		echo json_encode(['code'=>0,'count'=>$total,'data'=>$sms]);
	}

	//编辑模板内容显示
	public function editsmslist()
	{
		$id = $_GET['id'];
		$sql = "select * from mifi_sms where id = '{$id}'";
		$row = $this->db->query($sql)->row_array();
		echo json_encode(['code'=>1,'data'=>$row]);
	}

	//修改模板设置状态
	public function status()
	{
		$id = $this->input->post('id', true);
		$sms['status'] = $this->input->post('status', true);
		$editsms = $this->db->update('sms',  $sms, array('id'=> $id));
		if($editsms){  	
		    echo json_encode(['code'=>1,'infomsg'=>'修改成功']);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'修改失败']);
		}
	}

	//编辑模板设置
	public function editsms()
	{
		$edit['smsform'] = trim($this->input->post('smsform', true));
		$status = $this->input->post('status', true);
		if(empty($status)){
			$edit['status'] = 0;
		}else{
			$edit['status'] = 1;
		}
		$id = $this->input->post('id',true);
		$editsms = $this->db->update('sms',  $edit, array('id'=> $id));
		if($editsms){  	
		    echo json_encode(['code'=>1,'infomsg'=>'修改成功']);
		}else{
			echo json_encode(['code'=>0,'infomsg'=>'修改失败']);
		}
	}

	//单条记录删除短信模板
	public function del(){
		$id = $this->input->post('id',true);
		$this->db->where('id', $id);
        $del = $this->db->delete('sms'); 
        if($del){
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}

	//批量删除短信模板
	public function delall()
	{
		$id = $this->input->post('id',true);
		$this->db->where_in('id',$id);
        $delall = $this->db->delete('sms');
        if($delall){   	
           echo json_encode(['code'=>1,'infomsg'=>"删除成功"]);
        }else{
           echo json_encode(['code'=>0,'infomsg'=>"删除失败"]);
        }
	}
}
